package com.bkgy.controller.completeflow.service.impl;

import com.bkgy.controller.allprocess.entity.TbYieldRcl2150Entity;
import com.bkgy.controller.completeflow.dao.CompleteFlowDao;
import com.bkgy.controller.completeflow.entity.*;
import com.bkgy.controller.completeflow.service.CompleteFlowService;
import com.bkgy.sys.common.entity.ResultData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * 全流程成材率分析实现类
 */
@Service
public class CompleteFlowServiceImpl implements CompleteFlowService {
    @Autowired
    private CompleteFlowDao dao;

    /**
     * TBYIELDTCM = TB_YIELD_TCM = 酸轧
     */
    private static final String TBYIELDTCM = "TB_YIELD_TCM";

    /**
     * TBYIELDCGL = TB_YIELD_CGL =镀锌
     */
    private static final String TBYIELDCGL = "TB_YIELD_CGL";

    /**
     * TBYIELDCAL1630 = TB_YIELD_CAL1630 = 连退1630
     */
    private static final String TBYIELDCAL1630 = "TB_YIELD_CAL1630";

    /**
     * TBYIELDCAL2150 = TB_YIELD_CAL2150 = 连退2150
     */
    private static final String TBYIELDCAL2150 = "TB_YIELD_CAL2150";
    private static final String DATETYPE = "day";
    /**
     * TBYIELDRCL1630 = TB_YIELD_RCL1630 = 1630重卷
     */
    private static final String TBYIELDRCL1630 = "TB_YIELD_RCL1630";

    /**
     * TBYIELDRCL2150 = TB_YIELD_RCL2150 = 2150重卷
     */
    private static final String TBYIELDRCL2150 = "TB_YIELD_RCL2150";

    /**
     * TBYIELDRCL1870 = TB_YIELD_RCL1870 = 1870重卷
     */
    private static final String TBYIELDRCL1870 = "TB_YIELD_RCL1870";


    /**
     * 前台传入日期转L3日期格式
     * @param Times_db2 2025-09-04 00:00:00.000000
     * @return Times_L3 时间格式：20250703031218
     */
    public String setTimes_L3(String Times_db2) {
        String Times_L3 = "20250703031218";//时间格式：20250703031218
        try {
            String[] array_Time = Times_db2.split("-");//时间格式：2025-07-10-23:59:59.000000
            String str = array_Time[2].replace(" ", "");
            str = str.replace(":", "");
            String times = str.substring(0, 8);
            Times_L3 = array_Time[0] + array_Time[1] + times;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Times_L3;
    }


    /**
     * 前台传入日期转Rcl日期格式
     * @param front_Times  2025-09-04 00:00:00.000000
     * @return 时间格式：2025/6/01 00:00:01
     */
    public String setTimes_Rcl(String front_Times) {
        String Times_Rcl = "2025/09/10 09:13:11";//时间格式：2025/6/01 00:00:01
        try {
            String[] array_Time = front_Times.split("-");//时间格式：2025-06-01 00:00:01
            String times = array_Time[2].substring(0, 11);
            Times_Rcl = array_Time[0] +"/"+ array_Time[1] +"/"+ times;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Times_Rcl;
    }

    /**
     * 各产线成材率
     * @param request CompleteFlowRequest
     * @return ResultData
     */
    @Override
    public ResultData getCompleteFlowData(CompleteFlowRequest request) {

        //前端日期转L3格式
        request.setStartTimeL3(setTimes_L3(request.getStartTime()));
        request.setEndTimeL3(setTimes_L3(request.getEndTime()));
        //前端日期转Rcl格式
        request.setStartTimeRcl(setTimes_Rcl(request.getStartTime()));
        request.setEndTimeRcl(setTimes_Rcl(request.getEndTime()));

        try {
            List<CompleteFlowAcid> list = new ArrayList<>();
            if(StringUtils.isBlank(request.getDateType())){
                throw new IllegalArgumentException("：时间标识参数不能为空！");
            }
            boolean isDay = DATETYPE.equals(request.getDateType());
            if (TBYIELDTCM.equals(request.getType())) {
                list = isDay ? dao.queryTcmByDay(request)
                        : dao.queryTcmByMonth(request);
            }
            else if (TBYIELDCGL.equals(request.getType())) {
                list = isDay ? dao.queryCglByDay(request)
                        : dao.queryCglByMonth(request);
            }
            else if (TBYIELDCAL1630.equals(request.getType())) {
                list = isDay ? dao.queryCal1630ByDay(request)
                        : dao.queryCal1630ByMonth(request);
            }
            else if (TBYIELDCAL2150.equals(request.getType())) {
                list = isDay ? dao.queryCal2150ByDay(request)
                        : dao.queryCal2150ByMonth(request);
            }
            else if (TBYIELDRCL2150.equals(request.getType())) {
                list = isDay ? dao.queryCalRcl2150ByDay(request)
                        : dao.queryCalRcl2150ByMonth(request);
            }
            else if (TBYIELDRCL1630.equals(request.getType())) {
                list = isDay ? dao.queryCalRcl1630ByDay(request)
                        : dao.queryCalRcl1630ByMonth(request);
            }
            else if (TBYIELDRCL1870.equals(request.getType())) {
                list = isDay ? dao.queryCalRcl1870ByDay(request)
                        : dao.queryCalRcl1870ByMonth(request);
            }
            else
            {
                throw new IllegalArgumentException("产线标识参数type传递错误: " + request.getType());
            }
            return ResultData.ResultDataSuccess(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }

    /**
     * 全流程成材率分析班组统计
     * @param request CompleteFlowRequest
     * @return ResultData
     */
    @Override
    public ResultData getCompleteFlowCrewData(CompleteFlowRequest request) {
        //前端日期转L3格式
        request.setStartTimeL3(setTimes_L3(request.getStartTime()));
        request.setEndTimeL3(setTimes_L3(request.getEndTime()));
        //前端日期转Rcl格式
        request.setStartTimeRcl(setTimes_Rcl(request.getStartTime()));
        request.setEndTimeRcl(setTimes_Rcl(request.getEndTime()));

        try {
            List<CompleteFlowCrew> list = new ArrayList<>();
            if (TBYIELDTCM.equals(request.getType())) {
                list = dao.queryCrewTcm(request);
            }
            else if (TBYIELDCGL.equals(request.getType())) {
                list =  dao.queryCrewCgl(request);
            }
            else if (TBYIELDCAL1630.equals(request.getType())) {
                list = dao.queryCrewCal1630(request);
            }
            else if (TBYIELDCAL2150.equals(request.getType())) {
                list = dao.queryCrewCal2150(request);
            }
            else if (TBYIELDRCL1630.equals(request.getType())) {
                list = dao.queryCrewRcl1630(request);
            }
            else if (TBYIELDRCL2150.equals(request.getType())) {
                list = dao.queryCrewRcl2150(request);
            }
            else if (TBYIELDRCL1870.equals(request.getType())) {
                list = dao.queryCrewRcl1870(request);
            }
            else {
                throw new IllegalArgumentException("产线标识参数type传递错误: " + request.getType());
            }
            return ResultData.ResultDataSuccess(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }

    /**
     * 全流程成材率分析品规统计
     * @param request CompleteFlowRequest
     * @return ResultData
     */
    @Override
    public ResultData getCompleteFlowSpeciData(CompleteFlowRequest request) {
        //前端日期转L3格式
        request.setStartTimeL3(setTimes_L3(request.getStartTime()));
        request.setEndTimeL3(setTimes_L3(request.getEndTime()));
        //前端日期转Rcl格式
        request.setStartTimeRcl(setTimes_Rcl(request.getStartTime()));
        request.setEndTimeRcl(setTimes_Rcl(request.getEndTime()));

        try {
            List<CompleteFlowSpeci> list = new ArrayList<>();
            if (TBYIELDTCM.equals(request.getType())) {
                list = dao.querySpeciTcm(request);
                list = listUtils(list,request.getType());

            }
            else if (TBYIELDCGL.equals(request.getType())) {
                list =  dao.querySpeciCgl(request);
                list = listUtils(list,request.getType());
            }
            else if (TBYIELDCAL1630.equals(request.getType())) {
                list = dao.querySpeciCal1630(request);
                list = listUtils(list,request.getType());
            }
            else if (TBYIELDCAL2150.equals(request.getType())) {
                list = dao.querySpeciCal2150(request);
                list = listUtils(list,request.getType());
            }
            else if (TBYIELDRCL1630.equals(request.getType())) {
                list = dao.querySpeciRcl1630(request);
                list = listUtils(list,request.getType());
            }
            else if (TBYIELDRCL2150.equals(request.getType())) {
                list = dao.querySpeciRcl2150(request);
                list = listUtils(list,request.getType());
            }
            else if (TBYIELDRCL1870.equals(request.getType())) {
                list = dao.querySpeciRcl1870(request);
                list = listUtils(list,request.getType());
            }
            else {
                throw new IllegalArgumentException("产线标识参数type传递错误: " + request.getType());
            }
            return ResultData.ResultDataSuccess(list, list.size());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }

    /**
     * 全流程损失指标统计
     * @param request CompleteFlowRequest
     * @return ResultData
     */
    @Override
    public ResultData getCompleteFlowLossData(CompleteFlowRequest request) {
        //前端日期转L3格式
        request.setStartTimeL3(setTimes_L3(request.getStartTime()));
        request.setEndTimeL3(setTimes_L3(request.getEndTime()));
        //前端日期转Rcl格式
        request.setStartTimeRcl(setTimes_Rcl(request.getStartTime()));
        request.setEndTimeRcl(setTimes_Rcl(request.getEndTime()));

        try {
            List<CompleteFlowLoss> list = new ArrayList<>();
            if (TBYIELDTCM.equals(request.getType())) {
                list = dao.queryLossTcm(request);
            }
            else if (TBYIELDCGL.equals(request.getType())) {
                list =  dao.queryLossCgl(request);
            }
            else if (TBYIELDCAL1630.equals(request.getType())) {
                list = dao.queryLossCal1630(request);
            }
            else if (TBYIELDCAL2150.equals(request.getType())) {
                list = dao.queryLossCal2150(request);
            }
            else if (TBYIELDRCL1630.equals(request.getType())) {
                list = dao.queryLossRcl1630(request);
            }
            else if (TBYIELDRCL2150.equals(request.getType())) {
                list = dao.queryLossRcl2150(request);
            }
            else if (TBYIELDRCL1870.equals(request.getType())) {
                list = dao.queryLossRcl1870(request);
            }
            else
            {
                throw new IllegalArgumentException("产线标识参数type传递错误: " + request.getType());
            }
            return ResultData.ResultDataSuccess(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }


    /**
     * 处理分档显示
     * @param list
     * @return
     */
    public List listUtils(List<CompleteFlowSpeci> list,String type) {
        for (CompleteFlowSpeci flowSpeci : list) {
            BigDecimal exthkClass = flowSpeci.getExthkClass();
            BigDecimal widClass = flowSpeci.getWidClass();
            if(TBYIELDTCM.equals(type)||TBYIELDCGL.equals(type)||TBYIELDCAL2150.equals(type)||TBYIELDRCL1870.equals(type)||TBYIELDRCL2150.equals(type)){
                if (exthkClass != null) {
                    switch (exthkClass.intValue()) { // 转换为整数比较
                        case 1:
                            flowSpeci.setExthkSpeci(exthkClass+":0-0.3");
                            break;
                        case 2:
                            flowSpeci.setExthkSpeci(exthkClass+":0.3-0.35");
                            break;
                        case 3:
                            flowSpeci.setExthkSpeci(exthkClass+":0.35-0.4");
                            break;
                        case 4:
                            flowSpeci.setExthkSpeci(exthkClass+":0.4-0.5");
                            break;
                        case 5:
                            flowSpeci.setExthkSpeci(exthkClass+":0.5-0.6");
                            break;
                        case 6:
                            flowSpeci.setExthkSpeci(exthkClass+":0.6-0.8");
                            break;
                        case 7:
                            flowSpeci.setExthkSpeci(exthkClass+":0.8-1");
                            break;
                        case 8:
                            flowSpeci.setExthkSpeci(exthkClass+":1-1.2");
                            break;
                        case 9:
                            flowSpeci.setExthkSpeci(exthkClass+":1.2-1.5");
                            break;
                        case 10:
                            flowSpeci.setExthkSpeci(exthkClass+":1.5-1.8");
                            break;
                        case 11:
                            flowSpeci.setExthkSpeci(exthkClass+":18-2.1");
                            break;
                        case 12:
                            flowSpeci.setExthkSpeci(exthkClass+":2.1-2.5");
                            break;
                        case 13:
                            flowSpeci.setExthkSpeci(exthkClass+":2.5-2.6");
                            break;
                        case 14:
                            flowSpeci.setExthkSpeci(exthkClass+":2.6-9.9");
                            break;
                        case 15:
                            flowSpeci.setExthkSpeci(exthkClass+":9.9-10");
                            break;
                    }
                }

                if (widClass != null) {
                    switch (widClass.intValue()) {
                        case 1:
                            flowSpeci.setWidSpeci(widClass+":0-1000");
                            break;
                        case 2:
                            flowSpeci.setWidSpeci(widClass+":1000-1200");
                            break;
                        case 3:
                            flowSpeci.setWidSpeci(widClass+":1200-1400");
                            break;
                        case 4:
                            flowSpeci.setWidSpeci(widClass+":1400-1600");
                            break;
                        case 5:
                            flowSpeci.setWidSpeci(widClass+":1600-1800");
                            break;
                        case 6:
                            flowSpeci.setWidSpeci(widClass+":1800-2000");
                            break;
                        case 7:
                            flowSpeci.setWidSpeci(widClass+":2000-10000");
                            break;
                    }
                }
            }else if (TBYIELDCAL1630.equals(type)||TBYIELDRCL1630.equals(type)){
                if (exthkClass != null) {
                    switch (exthkClass.intValue()) { // 转换为整数比较
                        case 0:
                            flowSpeci.setExthkSpeci(exthkClass+":0-0.4");
                            break;
                        case 1:
                            flowSpeci.setExthkSpeci(exthkClass+":0.4-0.5");
                            break;
                        case 2:
                            flowSpeci.setExthkSpeci(exthkClass+":0.5-0.6");
                            break;
                        case 3:
                            flowSpeci.setExthkSpeci(exthkClass+":0.6-0.7");
                            break;
                        case 4:
                            flowSpeci.setExthkSpeci(exthkClass+":0.7-0.85");
                            break;
                        case 5:
                            flowSpeci.setExthkSpeci(exthkClass+":0.85-1");
                            break;
                        case 6:
                            flowSpeci.setExthkSpeci(exthkClass+":1-1.2");
                            break;
                        case 7:
                            flowSpeci.setExthkSpeci(exthkClass+":1.2-1.4");
                            break;
                        case 8:
                            flowSpeci.setExthkSpeci(exthkClass+":1.4-1.6");
                            break;
                        case 9:
                            flowSpeci.setExthkSpeci(exthkClass+":1.6-1.8");
                            break;
                        case 10:
                            flowSpeci.setExthkSpeci(exthkClass+":1.8-2");
                            break;
                        case 11:
                            flowSpeci.setExthkSpeci(exthkClass+":2-2.4");
                            break;
                        case 12:
                            flowSpeci.setExthkSpeci(exthkClass+":2.4-2.6");
                            break;
                        case 13:
                            flowSpeci.setExthkSpeci(exthkClass+":2.6-10");
                            break;
                    }
                }

                if (widClass != null) {
                    switch (widClass.intValue()) {
                        case 0:
                            flowSpeci.setWidSpeci(widClass + ":0-900");
                            break;
                        case 1:
                            flowSpeci.setWidSpeci(widClass + ":900-1000");
                            break;
                        case 2:
                            flowSpeci.setWidSpeci(widClass + ":1000-1100");
                            break;
                        case 3:
                            flowSpeci.setWidSpeci(widClass + ":1100-1200");
                            break;
                        case 4:
                            flowSpeci.setWidSpeci(widClass + ":1200-1300");
                            break;
                        case 5:
                            flowSpeci.setWidSpeci(widClass + ":1300-1400");
                            break;
                        case 6:
                            flowSpeci.setWidSpeci(widClass + ":1400-1500");
                            break;
                        case 7:
                            flowSpeci.setWidSpeci(widClass + ":1500-1600");
                            break;
                        case 8:
                            flowSpeci.setWidSpeci(widClass + ":1600-1700");
                            break;
                        case 9:
                            flowSpeci.setWidSpeci(widClass + ":1700-9999");
                            break;
                        case 10:
                            flowSpeci.setWidSpeci(widClass + ":9999-10000");
                            break;
                    }
                }
            }

        }
        return list;
    }
}
